package com.shamar.blog.service.impl;


import com.shamar.blog.dao.BlogAdminDao;
import com.shamar.blog.dto.BlogAdminExecution;
import com.shamar.blog.entity.BlogAdmin;
import com.shamar.blog.enums.BlogAdminStateEnum;
import com.shamar.blog.exceptions.BlogAdminOperationException;
import com.shamar.blog.service.BlogAdminService;
import com.shamar.blog.service.BlogAdminService;
import com.shamar.blog.util.FileUtil;
import com.shamar.blog.util.ImageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class BlogAdminServiceImpl implements BlogAdminService {
	@Autowired

	private BlogAdminDao blogAdminDao;
//	@Autowired
//	private ShopAuthMapDao shopAuthMapDao;
//	@Autowired
//	private ShopCategoryDao shopCategoryDao;


	@Override
	public BlogAdminExecution getBlogAdminByCondition(BlogAdmin BlogAdminCondition, int pageIndex,
                                             int pageSize) {
		//将pageindex转换为rowindex
//		int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
		//根据BlogAdminCondition查找BlogAdminList
//        if (pageIndex == null){
//            pageIndex = 1;
//        }
		BlogAdmin blogAdmin = blogAdminDao.selectByBlogAdminCondition(BlogAdminCondition,pageIndex,pageSize);
		BlogAdminExecution be = new BlogAdminExecution();
		if (blogAdmin != null){
			be.setBlogAdmin(blogAdmin);
			be.setState(BlogAdminStateEnum.NORMAL.getState());
			return be;
		}
		be.setState(BlogAdminStateEnum.NO_ADMIN.getState());
		be.setStateInfo(BlogAdminStateEnum.NO_ADMIN.getStateInfo());
		return be;
	}

//	@Override
//	public BlogAdminExecution addBlogAdmin(BlogAdmin BlogAdmin, InputStream BlogAdminImgInputStream, String fileName) throws BlogAdminOperationException {
//		//空值判断
//		if(BlogAdmin == null){
//			return new BlogAdminExecution(BlogAdminStateEnum.NULL_USER_INFO);
//		}
////			给用户信息赋值
//			BlogAdmin.setStatus(1);
//			BlogAdmin.setCreatetime(new Date());
//			BlogAdmin.setLastEdittime(new Date());
//				try {
//						//存储图片
//					if (BlogAdminImgInputStream != null) {
//						addBlogAdminImg(BlogAdmin, BlogAdminImgInputStream, fileName);
//					}
//						int effectedNum = blogAdminDao.insertSelective(BlogAdmin);
//						if (effectedNum > 0){
//							return new BlogAdminExecution(BlogAdminStateEnum.NORMAL);
//						}
////						//更新店铺的图片地址
////						effectedNum = blogAdminDao.updateShop(BlogAdmin);
//					return new BlogAdminExecution(BlogAdminStateEnum.NULL_USER_INFO);
//	}
//		catch (BlogAdminOperationException e){
//			throw new BlogAdminOperationException("insertUser error: " + e.getMessage());
//		}
//	}


//	@Override
//	public ShopExecution getByEmployeeId(long employeeId)
//			throws ShopOperationException {
//		List<Shop> shopList = shopDao.queryByEmployeeId(employeeId);
//		ShopExecution se = new ShopExecution();
//		se.setShopList(shopList);
//		return se;
//	}
//
//	@Override
//	public Shop getByShopId(long shopId) {
//		return shopDao.queryByShopId(shopId);
//	}
//	private void addShopImg(Shop shop, InputStream shopImgInputStream, String fileName) {
//		//获取shop图片目录的相对值路径
//		//用shopId作为图片存储的路径
//	String dest = FileUtil.getShopImagePath(shop.getShopId());
//	String shopImgAddr = ImageUtil.generateThumbnail(shopImgInputStream, fileName, dest);
////	String shopImgAddr = shopImgInputStream.getAbsolutePath();
//	shop.setShopImg(shopImgAddr);
//	}
//
//	@Override
//	@Transactional
//	/**
//	 * 使用注解控制事务方法的优点： 1.开发团队达成一致约定，明确标注事务方法的编程风格
//	 * 2.保证事务方法的执行时间尽可能短，不要穿插其他网络操作，RPC/HTTP请求或者剥离到事务方法外部
//	 * 3.不是所有的方法都需要事务，如只有一条修改操作，只读操作不需要事务控制
//	 */
//	public ShopExecution addShop(Shop shop, InputStream shopImgInputStream, String fileName)
//			throws ShopOperationException {
//		//空值判断
//		if (shop == null) {
//			return new ShopExecution(ShopStateEnum.NULL_SHOP_INFO);
//		}
//		try {
//			//给店铺信息赋初始值
//			shop.setEnableStatus(0);
//			shop.setCreateTime(new Date());
//			shop.setLastEditTime(new Date());
////			if (shopadmin.getShopCategory() != null) {
////				Long shopCategoryId = shopadmin.getShopCategory()
////						.getShopCategoryId();
////				ShopCategory sc = new ShopCategory();
//////				sc = shopCategoryDao.queryShopCategoryById(shopCategoryId);
////				ShopCategory parentCategory = new ShopCategory();
////				parentCategory.setShopCategoryId(sc.getParentId());
////				shopadmin.setParentCategory(parentCategory);
////			}
//			int effectedNum = shopDao.insertShop(shop);
//			if (effectedNum <= 0) {
//				throw new ShopOperationException("店铺创建失败");
//			} else {
//				try {
//					if (shopImgInputStream != null) {
//						//存储图片
//						addShopImg(shop, shopImgInputStream, fileName);
//						//更新店铺的图片地址
//						effectedNum = shopDao.updateShop(shop);
//						if (effectedNum <= 0) {
//							throw new ShopOperationException("创建图片地址失败");
//						}
//					}
//				} catch (Exception e) {
//					throw new ShopOperationException("addShopImg error: "
//							+ e.getMessage());
//				}
//				// 执行增加shopAuthMap操作
////				ShopAuthMap shopAuthMap = new ShopAuthMap();
////				shopAuthMap.setEmployeeId(shopadmin.getOwnerId());
////				shopAuthMap.setShopId(shopadmin.getShopId());
////				shopAuthMap.setName("");
////				shopAuthMap.setTitle("Owner");
////				shopAuthMap.setTitleFlag(1);
////				shopAuthMap.setCreateTime(new Date());
////				shopAuthMap.setLastEditTime(new Date());
////				shopAuthMap.setEnableStatus(1);
////				try {
////					effectedNum = shopAuthMapDao.insertShopAuthMap(shopAuthMap);
////					if (effectedNum <= 0) {
////						throw new ShopOperationException("授权创建失败");
////					} else {// 创建成功
////						return new ShopExecution(ShopStateEnum.CHECK, shopadmin);
////					}
////				} catch (Exception e) {
////					throw new ShopOperationException("insertShopAuthMap error: "
////							+ e.getMessage());
////				}
//				return new ShopExecution(ShopStateEnum.CHECK, shop);
//			}
//		}
//		catch (Exception e) {
//			throw new ShopOperationException("insertShop error: " + e.getMessage());
//		}
//	}
//
//	@Override
//	@Transactional
//	public ShopExecution modifyShop(Shop shopadmin, InputStream shopImgInputStream, String fileName)
//			throws ShopOperationException {
//		if (shopadmin == null || shopadmin.getShopId() == null) {
//			return new ShopExecution(ShopStateEnum.NULL_SHOPID);
//		} else {
//			try {
//				//1.判断是否需要处理图片
//				if (shopImgInputStream != null && fileName != null && !"".equals(fileName)) {
//					Shop tempShop = shopDao.queryByShopId(shopadmin.getShopId());
//					if (tempShop.getShopImg() != null) {
//						FileUtil.deleteFile(tempShop.getShopImg());
//					}
//					addShopImg(shopadmin, shopImgInputStream, fileName);
//				}
//				//2.更改店铺信息
//				shopadmin.setLastEditTime(new Date());
//				int effectedNum = shopDao.updateShop(shopadmin);
//				if (effectedNum <= 0) {
//					return new ShopExecution(ShopStateEnum.INNER_ERROR);
//				} else {// 操作成功
//					shopadmin = shopDao.queryByShopId(shopadmin.getShopId());
//					return new ShopExecution(ShopStateEnum.SUCCESS, shopadmin);
//				}
//			} catch (Exception e) {
//				throw new ShopOperationException("modifyShopCategory error: "
//						+ e.getMessage());
//			}
//		}
//	}



}
